package com.ubigate.CooknaviHA;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class TimerSetDlg extends Dialog implements OnClickListener {
	private static final String TAG = "TimerSetDlg";

	private Button minutes_add_btn;
	private Button seconds_add_btn;
	private Button minutes_sub_btn;
	private Button seconds_sub_btn;
	private Button timer_set_ok_btn;
	private Button timer_set_cancel_btn;
	
	private EditText minutes_text;
	private EditText seconds_text;

	private Integer nMinutes = 0;
	private Integer nSeconds = 0;
	
	private Boolean bOk = false;
	
	@SuppressWarnings("unused")
	private Context context;
	
	public TimerSetDlg(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
	}
	
	public TimerSetDlg(Context context, int theme) {
		super(context, theme);
		this.context = context;
	}

	public void setTime(Integer min, Integer sec) {
		Log.i(TAG, " setTime");
		
		nMinutes = min;
		nSeconds = sec;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, " onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timer_setup_dlg_v);
		
		minutes_add_btn = (Button) findViewById(R.id.minutes_add);
		seconds_add_btn = (Button) findViewById(R.id.seconds_add);
		minutes_sub_btn = (Button) findViewById(R.id.minutes_sub);
		seconds_sub_btn = (Button) findViewById(R.id.seconds_sub);
		timer_set_ok_btn = (Button) findViewById(R.id.timer_set_ok);
		timer_set_cancel_btn = (Button) findViewById(R.id.timer_set_cancel);
		
		minutes_text = (EditText) findViewById(R.id.minutes);
		seconds_text = (EditText) findViewById(R.id.seconds);
		
		minutes_add_btn.setOnClickListener(this);
		seconds_add_btn.setOnClickListener(this);
		minutes_sub_btn.setOnClickListener(this);
		seconds_sub_btn.setOnClickListener(this);
		timer_set_ok_btn.setOnClickListener(this);
		timer_set_cancel_btn.setOnClickListener(this);
		
		if (nMinutes > 0) minutes_text.setText(nMinutes.toString());
		if (nSeconds > 0) seconds_text.setText(nSeconds.toString());
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		String sTemp = "";
		int viewId = v.getId();
		
		switch (viewId) {
		case R.id.minutes_add:
			sTemp = minutes_text.getText().toString();
			if (sTemp.length() == 0) { nMinutes = 1; }
			else {
				nMinutes = Integer.parseInt(sTemp);
				nMinutes = nMinutes + 1;
			}
			minutes_text.setText(nMinutes.toString());
			break;
		case R.id.minutes_sub:
			sTemp = minutes_text.getText().toString();
			if (sTemp.length() == 0) { nMinutes = 0; }
			else {
				nMinutes = Integer.parseInt(sTemp);
				if (nMinutes > 0) nMinutes = nMinutes - 1;
			}
			minutes_text.setText(nMinutes.toString());
			break;
		case R.id.seconds_add:
			sTemp = seconds_text.getText().toString();
			if (sTemp.length() == 0) { nSeconds = 1; }
			else {
				nSeconds = Integer.parseInt(seconds_text.getText().toString());
				nSeconds = nSeconds + 1;
			}
			seconds_text.setText(nSeconds.toString());
			break;
		case R.id.seconds_sub:
			sTemp = seconds_text.getText().toString();
			if (sTemp.length() == 0) { nSeconds = 0; }
			else {
				nSeconds = Integer.parseInt(sTemp);
				if (nSeconds > 0) nSeconds = nSeconds - 1;
			}
			seconds_text.setText(nSeconds.toString());
			break;
		case R.id.timer_set_ok:
			sTemp = minutes_text.getText().toString();
			if (sTemp.length() == 0) { nMinutes = 0; }
			else {
				nMinutes = Integer.parseInt(sTemp);	
			}
			sTemp = seconds_text.getText().toString();
			if (sTemp.length() == 0) { nSeconds = 0; }
			else {
				nSeconds = Integer.parseInt(sTemp);
			}
			bOk = true; dismiss();
			break;
		case R.id.timer_set_cancel:
			bOk = false; dismiss();
			break;
		}
	}
	
	public Boolean getOk() {
		return bOk;
	}
	public Integer getMinutes() {
		return nMinutes;
	}
	public Integer getSeconds() {
		return nSeconds;
	}
}